/*******************
This display.c file is for STC90C52 MCU 
controlling the fan of some buildings


Functions:
control the lcd display,
based on the lcd driver(pcf8563.c) 

*******************/

extern void SendData(char dat);
extern void lcd_clear(void);
extern void lcd_allon(void);
void I2CSendByte(unsigned char ch);  

extern void bl55077_init(void);
extern bit WaitAck(void);
extern void I2CStop(void) ;
extern void delay(void);
extern void delayms(unsigned char ms);

//mask for LCD display
unsigned char mask[20]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
						0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

//to store 7 days' settings here
unsigned char g_set[7][24]={0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
						   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02
     						};

//clear all the LCD display
void display_all_clear(void)
{
unsigned char i=0;
	for(i=0;i<20;i++)
		mask[i]=0;
	lcd_clear();
}

//open all the LCD display
void display_all_on(void)
{
unsigned char i=0;
	for(i=0;i<20;i++)
		mask[i]=0xff;
	lcd_allon();
}

//not used
void display_time(unsigned char hour,unsigned char min,unsigned char sec)
{
	unsigned char i=0;
	unsigned char tmp1=0,tmp2=0;
	unsigned char sign[10]={0xfa,0x60,0xbc,0xf4,0x66,0xd6,0xde,0x70,0xfe,0xf6};
							//0,   1,	2,	 3,	  4,   5,   6,   7,   8,   9
	unsigned char sign2[10]={0xaf,0x06,0xcb,0x4f,0x66,0x6d,0xed,0x07,0xef,0x6f};

	tmp1=hour/10;
	mask[10]=mask[10]&0x01;
	tmp1=sign[tmp1]|mask[10];
	mask[10]=tmp1;

	tmp2=hour%10;
	tmp2=sign[tmp2];
	mask[11]=tmp2;

	bl55077_init();
	
	I2CSendByte(20); //display the hour
	WaitAck();

	I2CSendByte(tmp2);//second 8 of hour
	WaitAck();

	I2CSendByte(tmp1);//first 8 of hour
	WaitAck();

	I2CStop();	
////////////////////
	tmp2=sec%2;
	tmp2=tmp2<<4;
	SendData(tmp2);

	tmp1=min/10;
	tmp1=sign2[tmp1];
	tmp1=tmp1|tmp2;
	mask[0]=tmp1;

	tmp2=min%10;
	mask[1]=mask[1]&0x10;
	tmp2=sign2[tmp2]|mask[1];
	mask[1]=tmp2;
	
	bl55077_init();
	
	I2CSendByte(0); //display the min
	WaitAck();

	I2CSendByte(tmp1);//first 8 of min
	WaitAck();

	I2CSendByte(tmp2);//second 8 of min
	WaitAck();

	I2CStop();

}	  


void display_hour(unsigned char hour)
{

	unsigned char tmp1=0,tmp2=0;
	unsigned char sign[10]={0xfa,0x60,0xbc,0xf4,0x66,0xd6,0xde,0x70,0xfe,0xf6};
								//0,   1,	2,	 3,	  4,   5,   6,   7,   8,   9

	tmp1=hour%10;
	mask[10]=mask[10]&0x01;
	tmp1=sign[tmp1]|mask[10];
	mask[10]=tmp1;

	tmp2=hour/10;
	tmp2=sign[tmp2];
	mask[11]=tmp2;

	if(hour>24)	//if hour>24, then display ?? on the screen
	{
		tmp1=0;
		tmp2=0;
		mask[10]=mask[10]&0x01;
		mask[11]=0;
	}

	bl55077_init();
	
	I2CSendByte(20); //display the hour
	WaitAck();

	I2CSendByte(mask[10]);//second 8 of hour
	WaitAck();

	I2CSendByte(mask[11]);//first 8 of hour
	WaitAck();

	I2CStop();	
}

void display_min(unsigned char min,unsigned char sec)
{

	unsigned char tmp1=0,tmp2=0;
	unsigned char sign2[10]={0xaf,0x06,0xcb,0x4f,0x66,0x6d,0xed,0x07,0xef,0x6f};
							//0,   1,	2,	 3,	  4,   5,   6,   7,   8,   9

////////////////////
	tmp2=sec%2;
	tmp2=tmp2<<4;
//	SendData(tmp2);

	tmp1=min/10;
	tmp1=sign2[tmp1];
	tmp1=tmp1|tmp2;
	mask[0]=tmp1;

	tmp2=min%10;
	mask[1]=mask[1]&0x10;
	tmp2=sign2[tmp2]|mask[1];
	mask[1]=tmp2;

	if(min>60)	  //if min>60, then display ?? on the screen
	{
		mask[0]=mask[0]&0x10;
		mask[1]=mask[1]&0x10;
		tmp1=mask[0];
		tmp2=mask[1];
	}
	
	bl55077_init();
	
	I2CSendByte(0); //display the min
	WaitAck();

	I2CSendByte(mask[0]);//first 8 of min
	WaitAck();

	I2CSendByte(mask[1]);//second 8 of min
	WaitAck();

	I2CStop();
}

void display_week(unsigned char week) //display the week from 1 ~ 7,to indicate the date
{
unsigned char tmp;
	tmp=week&0x01;
	if(tmp==0x01)
		mask[13]=mask[13]|0x04;
	else
		mask[13]=mask[13]&0xfb;

	tmp=week&0x02;
	if(tmp==0x02)
		mask[14]=mask[14]|0x04;
	else
		mask[14]=mask[14]&0xfb;

	tmp=week&0x04;
	if(tmp==0x04)
		mask[15]=mask[15]|0x40;
	else
		mask[15]=mask[15]&0xbf;

	tmp=week&0x08;
	if(tmp==0x08)
		mask[15]=mask[15]|0x04;
	else
		mask[15]=mask[15]&0xfb;

	tmp=week&0x10;
	if(tmp==0x10)
		mask[17]=mask[17]|0x40;
	else
		mask[17]=mask[17]&0xbf;

	tmp=week&0x20;
	if(tmp==0x20)
		mask[18]=mask[18]|0x40;
	else
		mask[18]=mask[18]&0xbf;

	tmp=week&0x40;
	if(tmp==0x40)
		mask[19]=mask[19]|0x04;
	else
		mask[19]=mask[19]&0xfb;

 	bl55077_init();
	
	I2CSendByte(26); //base address of week
	WaitAck();

	I2CSendByte(mask[13]);//first
	WaitAck();

	I2CSendByte(mask[14]);//second
	WaitAck();

	I2CSendByte(mask[15]);//third+fouth 
	WaitAck();

	I2CSendByte(mask[16]);//null ,do not skip here for dislay speed
	WaitAck();

	I2CSendByte(mask[17]);//fifth 
	WaitAck();

	I2CSendByte(mask[18]);//sixth 
	WaitAck();

	I2CSendByte(mask[19]);//seventh 
	WaitAck();

	I2CStop();
		

}

//display the week and day option flag, these 2 options are used to control the running mode
void display_week_day(unsigned char week_flag,unsigned char day_flag)
{
	if(week_flag==1)
		mask[1]=mask[1]|0x10;
	else
		mask[1]=mask[1]&0xef;

	if(day_flag==1)
		mask[10]=mask[10]|0x01;
	else
		mask[10]=mask[10]&0xfe;


 	bl55077_init();
	
	I2CSendByte(2); //address of dp2
	WaitAck();

	I2CSendByte(mask[1]);//week flag
	WaitAck();

	I2CStop();	

 	bl55077_init();

	I2CSendByte(20); //address of dp1
	WaitAck();

	I2CSendByte(mask[10]);//day flag
	WaitAck();

	I2CStop();	

}

//display the 24 setting of current day
void display_setting(unsigned char pos,unsigned char mark)
{

unsigned char tmp=0;
unsigned char mark1=0;
unsigned char mark2=0;
unsigned char mark3=0;

	if(mark==1)
		mark1=0x00;
	if(mark==2)
		mark1=0x20;
	if(mark==3)
		mark1=0x21;
	if(mark==4)
		mark1=0x31;
	if(mark==5)
		mark1=0x33;

	if(mark==1)
		mark2=0x00;
	if(mark==2)
		mark2=0x10;
	if(mark==3)
		mark2=0x12;
	if(mark==4)
		mark2=0x32;
	if(mark==5)
		mark2=0x33;

	if(mark==1)
		mark3=0x00;
	if(mark==2)
		mark3=0x40;
	if(mark==3)
		mark3=0x48;
	if(mark==4)
		mark3=0xc8;
	if(mark==5)
		mark3=0xcc;

	if(pos==1)
	{
		bl55077_init();
		I2CSendByte(24); 
		WaitAck();

		mask[12]=mask[12]&0xcc;
		tmp=mask[12]|mark1;
		mask[12]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;

	}

	if(pos==2)
	{
		bl55077_init();
		I2CSendByte(26); 
		WaitAck();

		mask[13]=mask[13]&0xcc;
		tmp=mask[13]|mark1;
		mask[13]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==3)
	{
		bl55077_init();
		I2CSendByte(28); 
		WaitAck();

		mask[14]=mask[14]&0xcc;
		tmp=mask[14]|mark1;
		mask[14]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==4)
	{
		bl55077_init();
		I2CSendByte(30); 
		WaitAck();

		mask[15]=mask[15]&0xcc;
		tmp=mask[15]|mark1;
		mask[15]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==5)
	{
		bl55077_init();
		I2CSendByte(32); 
		WaitAck();

		mask[16]=mask[16]&0xcc;
		tmp=mask[16]|mark1;
		mask[16]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==6)
	{
		bl55077_init();
		I2CSendByte(34); 
		WaitAck();

		mask[17]=mask[17]&0xcc;
		tmp=mask[17]|mark1;
		mask[17]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==7)
	{
		bl55077_init();
		I2CSendByte(36); 
		WaitAck();

		mask[18]=mask[18]&0xcc;
		tmp=mask[18]|mark1;
		mask[18]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==8)
	{
		bl55077_init();
		I2CSendByte(38); 
		WaitAck();

		mask[19]=mask[19]&0xcc;
		tmp=mask[19]|mark1;
		mask[19]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==9)
	{
		bl55077_init();
		I2CSendByte(4); 
		WaitAck();

		mask[2]=mask[2]&0xcc;
		tmp=mask[2]|mark2;
		mask[2]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==10)
	{
		bl55077_init();
		I2CSendByte(6); 
		WaitAck();

		mask[3]=mask[3]&0xcc;
		tmp=mask[3]|mark2;
		mask[3]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==11)
	{
		bl55077_init();
		I2CSendByte(8); 
		WaitAck();

		mask[4]=mask[4]&0xcc;
		tmp=mask[4]|mark2;
		mask[4]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==12)
	{
		bl55077_init();
		I2CSendByte(10); 
		WaitAck();

		mask[5]=mask[5]&0xcc;
		tmp=mask[5]|mark2;
		mask[5]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==13)
	{
		bl55077_init();
		I2CSendByte(12); 
		WaitAck();

		mask[6]=mask[6]&0xcc;
		tmp=mask[6]|mark2;
		mask[6]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==14)
	{
		bl55077_init();
		I2CSendByte(14); 
		WaitAck();

		mask[7]=mask[7]&0xcc;
		tmp=mask[7]|mark2;
		mask[7]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==15)
	{
		bl55077_init();
		I2CSendByte(16); 
		WaitAck();

		mask[8]=mask[8]&0xcc;
		tmp=mask[8]|mark2;
		mask[8]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==16)
	{
		bl55077_init();
		I2CSendByte(18); 
		WaitAck();

		mask[9]=mask[9]&0xcc;
		tmp=mask[9]|mark2;
		mask[9]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==17)
	{
		bl55077_init();
		I2CSendByte(4); 
		WaitAck();

		mask[2]=mask[2]&0x33;
		tmp=mask[2]|mark3;
		mask[2]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==18)
	{
		bl55077_init();
		I2CSendByte(6); 
		WaitAck();

		mask[3]=mask[3]&0x33;
		tmp=mask[3]|mark3;
		mask[3]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==19)
	{
		bl55077_init();
		I2CSendByte(8); 
		WaitAck();

		mask[4]=mask[4]&0x33;
		tmp=mask[4]|mark3;
		mask[4]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==20)
	{
		bl55077_init();
		I2CSendByte(10); 
		WaitAck();

		mask[5]=mask[5]&0x33;
		tmp=mask[5]|mark3;
		mask[5]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==21)
	{
		bl55077_init();
		I2CSendByte(12); 
		WaitAck();

		mask[6]=mask[6]&0x33;
		tmp=mask[6]|mark3;
		mask[6]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==22)
	{
		bl55077_init();
		I2CSendByte(14); 
		WaitAck();

		mask[7]=mask[7]&0x33;
		tmp=mask[7]|mark3;
		mask[7]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==23)
	{
		bl55077_init();
		I2CSendByte(16); 
		WaitAck();

		mask[8]=mask[8]&0x33;
		tmp=mask[8]|mark3;
		mask[8]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}

	if(pos==24)
	{
		bl55077_init();
		I2CSendByte(18); 
		WaitAck();

		mask[9]=mask[9]&0x33;
		tmp=mask[9]|mark3;
		mask[9]=tmp;

		I2CSendByte(tmp);
		WaitAck();
	
		I2CStop();
		return;	
	}



}

//display the 5 marks, which are used to indicate the current setting
void display_current_setting(unsigned char flag1,unsigned char flag2,unsigned char flag3,unsigned char flag4,unsigned char flag5)
{
unsigned char tmp;
	bl55077_init();
	tmp=flag1<<6;
	mask[12]=mask[12]&0xbf;
	tmp=tmp|mask[12];
	mask[12]=tmp;

 	I2CSendByte(24);   		//first flag
 	WaitAck(); 
	I2CSendByte(tmp);
	WaitAck();

	tmp=flag2<<6;			//second flag
	mask[13]=mask[13]&0xbf;
	tmp=tmp|mask[13];
	mask[13]=tmp;
	I2CSendByte(tmp);
	WaitAck();
	I2CStop();

	bl55077_init();
	I2CSendByte(30);
	WaitAck();
	tmp=flag3<<7;			//third flag
	mask[15]=mask[15]&0x7f;
	tmp=tmp|mask[15];
	mask[15]=tmp;
	I2CSendByte(tmp);
	WaitAck();

	tmp=flag4<<2;		   //fouth flag
	mask[16]=mask[16]&0xfb;	
	tmp=tmp|mask[16];
	mask[16]=tmp;
	I2CSendByte(tmp);
	WaitAck();

	I2CStop();

	bl55077_init();
	I2CSendByte(36); 		//fifth flag
	WaitAck();
	tmp=flag5<<2;	
	mask[18]=mask[18]&0xfb;
	tmp=tmp|mask[18];
	mask[18]=tmp;
	I2CSendByte(tmp);
	WaitAck();

	I2CStop();
}

//display the current 5 fans(4 leaves) on the lcd top, which are used to indicate 
//fan control state
void display_current_fan(unsigned char flag1,unsigned char flag2,unsigned char flag3,unsigned char flag4,unsigned char flag5)
{
	unsigned char tmp=0;
	unsigned char tmp1=0;
	unsigned char tmp2=0;

//first flag
	if(flag1==1)
	{
	mask[13]=mask[13]&0x7f;

	mask[12]=mask[12]|0x8c;
	}
	if(flag1==2)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x88;

	mask[13]=mask[13]|0x80;
	}
	if(flag1==3)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x0c;

	mask[13]=mask[13]|0x80;
	}
	if(flag1==4)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x84;

	mask[13]=mask[13]|0x80;
	}
	if(flag1==5)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x8c;

	mask[13]=mask[13]&0x7f;
	mask[13]=0x80|mask[13];
	}
	if(flag1==0)
	{
	mask[12]=mask[12]&0x73;

	mask[13]=mask[13]&0x7f;
	}
	//display
	bl55077_init();
 	I2CSendByte(24);   		
 	WaitAck(); 
	I2CSendByte(mask[12]);
	WaitAck();
	I2CSendByte(mask[13]);
	WaitAck();
	I2CStop();

//second flag
	if(flag2==1)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0xc0|mask[14];

	mask[13]=mask[13]|0x08;
	}
	if(flag2==2)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0x88|mask[14];

	mask[13]=mask[13]|0x08;
	}
	if(flag2==3)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0xc8|mask[14];

	mask[13]=mask[13]&0xf7;
	}
	if(flag2==4)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0x48|mask[14];

	mask[13]=mask[13]|0x08;
	}
	if(flag2==5)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0xc8|mask[14];

	mask[13]=mask[13]&0xf7;
	mask[13]=0x08|mask[13];
	}
	if(flag2==0)
	{
	mask[14]=mask[14]&0x37;

	mask[13]=mask[13]&0xf7;
	}

	//display
	bl55077_init();
 	I2CSendByte(26);   		
 	WaitAck(); 
	I2CSendByte(mask[13]);
	WaitAck();
	I2CSendByte(mask[14]);
	WaitAck();
	I2CStop();

//third flag
	if(flag3==1)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0xc0|mask[16];

	mask[15]=mask[15]|0x08;
	}
	if(flag3==2)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0x88|mask[16];

	mask[15]=mask[15]|0x08;
	}
	if(flag3==3)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0xc8|mask[16];

	mask[15]=mask[15]&0xf7;

	}
	if(flag3==4)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0x48|mask[16];

	mask[15]=mask[15]|0x08;
	}
	if(flag3==5)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0xc8|mask[16];

	mask[15]=mask[15]&0xf7;
	mask[15]=mask[15]|0x08;
	}
	if(flag3==0)
	{
	mask[16]=mask[16]&0x37;

	mask[15]=mask[15]&0xf7;
	}
	//display
	bl55077_init();
 	I2CSendByte(30);   		
 	WaitAck(); 
	I2CSendByte(mask[15]);
	WaitAck();
	I2CSendByte(mask[16]);
	WaitAck();
	I2CStop();

//fourth flag	
	if(flag4==1)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x8c;
	}
	if(flag4==2)
	{
	mask[18]=mask[18]|0x80;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x88;
	}
	if(flag4==3)
	{
	mask[18]=mask[18]|0x80;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x0c;
	}
	if(flag4==4)
	{
	mask[18]=mask[18]|0x80;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x84;
	}
	if(flag4==5)
	{
	mask[18]=mask[18]&0x7f;
	mask[18]=mask[18]|0x80;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x8c;
	}
	if(flag4==0)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	}

	//display
	bl55077_init();
 	I2CSendByte(34);   		
 	WaitAck(); 
	I2CSendByte(mask[17]);
	WaitAck();
	I2CSendByte(mask[18]);
	WaitAck();
	I2CStop();

//fifth flag
	if(flag5==1)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0xc0|mask[19];

	mask[18]=mask[18]|0x08;
	}
	if(flag5==2)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0x88|mask[19];

	mask[18]=mask[18]|0x08;
	}
	if(flag5==3)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0xc8|mask[19];

	mask[18]=mask[18]&0xf7;
	}
	if(flag5==4)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0x48|mask[19];

	mask[18]=mask[18]|0x08;
	}
	if(flag5==5)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0xc8|mask[19];

	mask[18]=mask[18]&0xf7;
	mask[18]=mask[18]|0x08;
	}
	if(flag5==0)
	{
	mask[19]=mask[19]&0x37;

	mask[18]=mask[18]&0xf7;
	}
	//display
	bl55077_init();
 	I2CSendByte(36);   		
 	WaitAck(); 
	I2CSendByte(mask[18]);
	WaitAck();
	I2CSendByte(mask[19]);
	WaitAck();
	I2CStop();
/*qqqq
//first flag
	if(flag1==1)
	{
	mask[13]=mask[13]&0x7f;
	tmp=0x80|mask[13];
	mask[13]=tmp;

	mask[12]=mask[12]&0x73;
	}
	if(flag1==2)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x04;

	mask[13]=mask[13]&0x7f;
	}
	if(flag1==3)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x80;

	mask[13]=mask[13]&0x7f;
	}
	if(flag1==4)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x08;

	mask[13]=mask[13]&0x7f;
	}
	if(flag1==5)
	{
	mask[12]=mask[12]&0x73;
	mask[12]=mask[12]|0x8c;

	mask[13]=mask[13]&0x7f;
	mask[13]=0x80|mask[13];
	}
	if(flag1==0)
	{
	mask[12]=mask[12]&0x73;

	mask[13]=mask[13]&0x7f;
	}
	//display
	bl55077_init();
 	I2CSendByte(24);   		
 	WaitAck(); 
	I2CSendByte(mask[12]);
	WaitAck();
	I2CSendByte(mask[13]);
	WaitAck();
	I2CStop();

//second flag
	if(flag2==1)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0x08|mask[14];

	mask[13]=mask[13]&0xf7;
	}
	if(flag2==2)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0x40|mask[14];

	mask[13]=mask[13]&0xf7;
	}
	if(flag2==3)
	{
	mask[14]=mask[14]&0x37;

	mask[13]=mask[13]&0xf7;
	mask[13]=mask[13]|0x08;
	}
	if(flag2==4)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0x80|mask[14];

	mask[13]=mask[13]&0xf7;
	}
	if(flag2==5)
	{
	mask[14]=mask[14]&0x37;
	mask[14]=0xc8|mask[14];

	mask[13]=mask[13]&0xf7;
	mask[13]=0x08|mask[13];
	}
	if(flag2==0)
	{
	mask[14]=mask[14]&0x37;

	mask[13]=mask[13]&0xf7;
	}

	//display
	bl55077_init();
 	I2CSendByte(26);   		
 	WaitAck(); 
	I2CSendByte(mask[13]);
	WaitAck();
	I2CSendByte(mask[14]);
	WaitAck();
	I2CStop();

//third flag
	if(flag3==1)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0x08|mask[16];

	mask[15]=mask[15]&0xf7;
	}
	if(flag3==2)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0x40|mask[16];

	mask[15]=mask[15]&0xf7;
	}
	if(flag3==3)
	{
	mask[16]=mask[16]&0x37;

	mask[15]=mask[15]&0xf7;
	mask[15]=mask[15]|0x08;
	}
	if(flag3==4)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0x80|mask[16];

	mask[15]=mask[15]&0xf7;
	}
	if(flag3==5)
	{
	mask[16]=mask[16]&0x37;
	mask[16]=0xc8|mask[16];

	mask[15]=mask[15]&0xf7;
	mask[15]=mask[15]|0x08;
	}
	if(flag3==0)
	{
	mask[16]=mask[16]&0x37;

	mask[15]=mask[15]&0xf7;
	}
	//display
	bl55077_init();
 	I2CSendByte(30);   		
 	WaitAck(); 
	I2CSendByte(mask[15]);
	WaitAck();
	I2CSendByte(mask[16]);
	WaitAck();
	I2CStop();

//fourth flag	
	if(flag4==1)
	{
	mask[18]=mask[18]&0x7f;
	mask[18]=0x80|mask[18];

	mask[17]=mask[17]&0x73;
	}
	if(flag4==2)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x04;
	}
	if(flag4==3)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x80;
	}
	if(flag4==4)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x08;
	}
	if(flag4==5)
	{
	mask[18]=mask[18]&0x7f;
	mask[18]=mask[18]|0x80;

	mask[17]=mask[17]&0x73;
	mask[17]=mask[17]|0x8c;
	}
	if(flag4==0)
	{
	mask[18]=mask[18]&0x7f;

	mask[17]=mask[17]&0x73;
	}

	//display
	bl55077_init();
 	I2CSendByte(34);   		
 	WaitAck(); 
	I2CSendByte(mask[17]);
	WaitAck();
	I2CSendByte(mask[18]);
	WaitAck();
	I2CStop();

//fifth flag
	if(flag5==1)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0x08|mask[19];

	mask[18]=mask[18]&0xf7;
	}
	if(flag5==2)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0x40|mask[19];

	mask[18]=mask[18]&0xf7;
	}
	if(flag5==3)
	{
	mask[19]=mask[19]&0x37;

	mask[18]=mask[18]&0xf7;
	mask[18]=mask[18]|0x80;
	}
	if(flag5==4)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0x80|mask[19];

	mask[18]=mask[18]&0xf7;
	}
	if(flag5==5)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0xc8|mask[19];

	mask[18]=mask[18]&0xf7;
	mask[18]=mask[18]|0x08;
	}
	if(flag5==0)
	{
	mask[19]=mask[19]&0x37;
	mask[19]=0xc8|mask[19];

	mask[18]=mask[18]&0xf7;
	mask[18]=mask[18]|0x08;
	}
	//display
	bl55077_init();
 	I2CSendByte(36);   		
 	WaitAck(); 
	I2CSendByte(mask[18]);
	WaitAck();
	I2CSendByte(mask[19]);
	WaitAck();
	I2CStop();
qqqq*/

/*
//first flag
	if(flag1==1)
	{
	mask[12]=mask[12]&0x73;
	tmp=0x08|mask[12];
	mask[12]=tmp;
	}
	if(flag1==2)
	{
	mask[12]=mask[12]&0x73;
	tmp=0x04|mask[12];
	mask[12]=tmp;
	}
	if(flag1==3)
	{
	mask[12]=mask[12]&0x73;
	tmp=0x80|mask[12];	
	mask[12]=tmp;
	}
	if(flag1==4)
	{
	mask[12]=mask[12]&0x73;
	tmp=0x8c|mask[12];	
	mask[12]=tmp;
	}
	if(flag1==0)
	{
	mask[12]=0x73&mask[12];
	tmp=mask[12];
	}

 	I2CSendByte(24);   		
 	WaitAck(); 
	I2CSendByte(tmp);
	WaitAck();

//second flag
	if(flag2==1)
	{
	mask[13]=mask[13]&0x73;
	tmp=0x08|mask[13];
	mask[13]=tmp;
	}
	if(flag2==2)
	{
	mask[13]=mask[13]&0x73;
	tmp=0x04|mask[13];
	mask[13]=tmp;
	}
	if(flag2==3)
	{
	mask[13]=mask[13]&0x73;
	tmp=0x80|mask[13];	
	mask[13]=tmp;
	}
	if(flag2==4)
	{
	mask[13]=mask[13]&0x73;
	tmp=0x8c|mask[13];	
	mask[13]=tmp;
	}
	if(flag2==0)
	{
	mask[13]=0x73&mask[13];
	tmp=mask[13];
	}

	I2CSendByte(tmp);
	WaitAck();

//third flag
	if(flag3==1)
	{
	tmp1=0xf7&mask[14];
	tmp1=0|tmp1;
	mask[14]=tmp1;
	tmp2=0x3f&mask[15];
	tmp2=0x80|tmp2;
	mask[15]=tmp2;
	}
	if(flag3==2)
	{
	tmp1=0xf7&mask[14];
	tmp1=0|tmp1;
	mask[14]=tmp1;
	tmp2=0x3f&mask[15];
	tmp2=0x40|tmp2;	
	mask[15]=tmp2;
	}
	if(flag3==3)
	{
	tmp1=0xf7&mask[14];
	tmp1=0x08|tmp1;
	mask[14]=tmp1;
	tmp2=0x3f&mask[15];
	tmp2=0x00|tmp2;		
	mask[15]=tmp2;
	}
	if(flag3==4)
	{
	tmp1=0xf7&mask[14];
	tmp1=0x08|tmp1;
	mask[14]=tmp1;
	tmp2=0x3f&mask[15];
	tmp2=0xc0|tmp2;		
	mask[15]=tmp2;
	}
	if(flag3==0)
	{
	tmp1=0xf7&mask[14];
	tmp1=0x00|tmp1;
	mask[14]=tmp1;
	tmp2=0x3f&mask[15];
	tmp2=0x00|tmp2;	
	mask[15]=tmp2;
	}
	I2CSendByte(tmp1);
	WaitAck();
	I2CSendByte(tmp2);
	WaitAck();

//fourth flag	
	if(flag4==1)
	{
	tmp=mask[16]&0x73;
	tmp=0x08|tmp;
	mask[16]=tmp;
	}
	if(flag4==2)
	{
	tmp=mask[16]&0x73;
	tmp=0x04|tmp;
	mask[16]=tmp;
	}
	if(flag4==3)
	{
	tmp=mask[16]&0x73;
	tmp=0x80|tmp;	
	mask[16]=tmp;
	}
	if(flag4==4)
	{
	tmp=mask[16]&0x73;
	tmp=0x8c|tmp;
	mask[16]=tmp;
	}
	if(flag4==0)
	{
	tmp=0x73&mask[16];
	mask[16]=tmp;
	}

	I2CSendByte(tmp);
	WaitAck();
	I2CStop();

//fifth flag
	if(flag5==1)
	{
	tmp1=0xf7&mask[18];
	tmp1=0|tmp1;
	mask[18]=tmp1;
	tmp2=0x3f&mask[19];
	tmp2=0x80|tmp2;
	mask[19]=tmp2;
	}
	if(flag5==2)
	{
	tmp1=0xf7&mask[18];
	tmp1=0|tmp1;
	mask[18]=tmp1;
	tmp2=0x3f&mask[19];
	tmp2=0x40|tmp2;	
	mask[19]=tmp2;
	}
	if(flag5==3)
	{
	tmp1=0xf7&mask[18];
	tmp1=0x08|tmp1;
	mask[18]=tmp1;
	tmp2=0x3f&mask[19];
	tmp2=0x00|tmp2;		
	mask[19]=tmp2;
	}
	if(flag5==4)
	{
	tmp1=0xf7&mask[18];
	tmp1=0x08|tmp1;
	mask[18]=tmp1;
	tmp2=0x3f&mask[19];
	tmp2=0xc0|tmp2;		
	mask[19]=tmp2;
	}
	if(flag5==0)
	{
	tmp1=0xf7&mask[18];
	tmp1=0x00|tmp1;
	mask[18]=tmp1;
	tmp2=0x3f&mask[19];
	tmp2=0x00|tmp2;	
	mask[19]=tmp2;
	}
*/

}



void display_test1(void)
{
	unsigned char count3=0;

	display_current_fan(1,0,0,0,0);
	for(count3=0;count3<8;count3++)
		delay();

	display_current_fan(2,0,0,0,0);
	for(count3=0;count3<8;count3++)
		delay();

	display_current_fan(3,0,0,0,0);
	for(count3=0;count3<8;count3++)
		delay();

	display_current_fan(4,0,0,0,0);
	for(count3=0;count3<8;count3++)
		delay();

	display_current_fan(0,0,0,0,0);
	for(count3=0;count3<10;count3++)
		delay();

	display_current_fan(0,1,0,0,0);
	for(count3=0;count3<10;count3++)
		delay();

	display_current_fan(0,2,0,0,0);
	for(count3=0;count3<10;count3++)
		delay();

	display_current_fan(0,3,0,0,0);
	for(count3=0;count3<10;count3++)
		delay();


	display_current_fan(0,4,0,0,0);
	for(count3=0;count3<10;count3++)
		delay();


	display_current_fan(0,0,0,0,0);
	for(count3=0;count3<20;count3++)
		delay();

	display_current_fan(0,0,1,0,0);
	for(count3=0;count3<20;count3++)
		delay();

	display_current_fan(0,0,2,0,0);
	for(count3=0;count3<20;count3++)
		delay();

	display_current_fan(0,0,3,0,0);
	for(count3=0;count3<20;count3++)
		delay();

	display_current_fan(0,0,4,0,0);
	for(count3=0;count3<20;count3++)
		delay();

	display_current_fan(0,0,0,0,0);
	for(count3=0;count3<20;count3++)
		delay();
while(1)
{
	display_current_fan(0,0,0,1,0);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,2,0);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,3,0);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,4,0);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,0);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,1);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,2);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,3);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,4);
	for(count3=0;count3<40;count3++)
		delay();

	display_current_fan(0,0,0,0,0);
	for(count3=0;count3<40;count3++)
		delay();
}



}




void display_test(void)
{
	unsigned char count3=0;
	unsigned char i=0;
	unsigned char j=0;
	unsigned char tmp=0;

for(j=0;j<3;j++)
{
 	for(tmp=0;tmp<24;tmp++)
		display_setting(tmp+1,5);
	for(i=0;i<10;i++)
 		delayms(250);

	for(tmp=0;tmp<24;tmp++)
		display_setting(tmp+1,1);
	for(i=0;i<10;i++)
		delayms(250);
}
/*
					//flash all mod set directly					
						for(i=0;i<30;i++)
							delay();
						for(j=0;j<24;j++)
							display_setting(j+1,1);


						for(i=0;i<30;i++)
							delay();
						for(j=0;j<24;j++)
							display_setting(j+1,5);


						for(i=0;i<30;i++)
							delay();
						for(j=0;j<24;j++);
							display_setting(j+1,1);


						for(i=0;i<30;i++)
							delay();
						for(j=0;j<24;j++)
							display_setting(j+1,5);


						for(i=0;i<30;i++)
							delay();
*/
//						for(j=0;j<24;j++)
//						display_setting(j+1,g_set[g_week][j]);




}


unsigned char bcd2dec(unsigned char bcd)
{
unsigned char tmp=0;
unsigned char tmp1=0;
unsigned char tmp2=0;
	tmp1=bcd/16;
	tmp2=bcd%16;
	tmp=tmp1*10+tmp2;
	return tmp;
}

unsigned char dec2bcd(unsigned char dec)
{
unsigned char tmp=0;
unsigned char tmp1=0;
unsigned char tmp2=0;
	tmp1=dec/10;	  
	tmp2=dec%10;
	tmp=tmp1*16+tmp2;
	return tmp;
}









